package tp.pr3.instructions;

import tp.pr3.Message;
import tp.pr3.NavigationModule;
import tp.pr3.RobotEngine;
import tp.pr3.instructions.exceptions.*;
import tp.pr3.items.ItemContainer;

public class QuitInstruction implements Instruction {

	private RobotEngine engine;
	
	
	/**
	 * It builds a default QuitInstruction
	 */
	public QuitInstruction() {

	}

	@Override
	/**
	 * Parsers the String returning an instance of QuitInstruction or throwing a WrongInstructionFormatException()
	 * cad - text String to parse
	 * @returns Instruction reference to an instance of QuitInstruction
	 * @throws WrongInstructionFormatException - When the String is not QUIT | SALIR
	 */
	public Instruction parse(String cad) throws WrongInstructionFormatException {
		for (String i : Message.QUIT_INSTRUCTION) {
			if (cad.equalsIgnoreCase(i))
				return new QuitInstruction();
		}
		throw new WrongInstructionFormatException(Message.NOT_UNDERSTAND);
	} // puede que haya que cambiar el mensaje de la excepcion

	/**
	 * Returns a description of the Instruction syntax. The string does not end
	 * with the line separator. It is up to the caller adding it before
	 * printing.
	 * 
	 */
	public String getHelp() {
		String help = Message.QUIT_INSTRUCTION[0];
		for (int i = 1; i < Message.QUIT_INSTRUCTION.length; i++)
			help += "|" + Message.QUIT_INSTRUCTION[i];

		return help;
	}

	@Override
	/**
	 * Set the execution context. The method receives the entire engine (engine,
	 * navigation and the robot container) even though the actual implementation
	 * of execute() may not require it.
	 * @param engine - The robot engine
	 * @param navigation - The information about the game, i.e., the places, current direction and current heading to navigate
	 * @param robotContainer - The inventory of the robot
	 * 
	 */
	public void configureContext(RobotEngine engine,
			NavigationModule navigation, ItemContainer robotContainer) {
		this.engine = engine;

	}

	/**
	 * Request the robot to stop the simulation
	 * 
	 * @throws InstructionExecutionException
	 *             - if there exist any execution error.
	 */
	public void execute() throws InstructionExecutionException {

		engine.requestQuit();
		System.out.println(Message.COM_PROBLEMS);

	}

}
